package cn.iocoder.yudao.framework.business.basic.convert;

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  {@link BigDecimal} 保留两位小数，调用：{@code @Mapper(uses = {BigDecimalConvert.class})}
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/04/08
 */
@Mapper
public interface BigDecimalConvert {
    BigDecimalConvert INSTANCE = Mappers.getMapper(BigDecimalConvert.class);

    default BigDecimal convert(BigDecimal from) {
        if (from == null) {
            from = BigDecimal.ZERO;
        }
        return from.setScale(2, RoundingMode.HALF_UP);
    }

    default BigDecimal convert(String from) {
        if (from == null) {
            from = "0";
        }
        return convert(new BigDecimal(from));
    }

    default BigDecimal convert(Integer from) {
        if (from == null) {
            from = 0;
        }
        return convert(new BigDecimal(from));
    }

    default BigDecimal convert(Long from) {
        if (from == null) {
            from = 0L;
        }
        return convert(new BigDecimal(from));
    }

    default BigDecimal convert(Float from) {
        if (from == null) {
            from = 0F;
        }
        return convert(new BigDecimal(from));
    }

    default BigDecimal convert(Double from) {
        if (from == null) {
            from = 0D;
        }
        return convert(new BigDecimal(from));
    }

}